﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IllustratedCsharp7_ch15_StandardEvent
{
    class Incrementer
    {
        public event EventHandler CountedADozen; //使用系统定义的EventHandler委托
        public void DoCount()
        {
            for (int i = 1; i < 100; i++)
                if (i % 12 == 0 && CountedADozen != null)
                    CountedADozen(this,null); //触发事件时使用EventHandler的参数
        }
    }

    class Dozens
    {
        public int DozensCount { get; private set; }
        public Dozens(Incrementer incrementer)
        {
            DozensCount = 0;
            incrementer.CountedADozen += IncrementDozensCount; //订阅事件
        }

        void IncrementDozensCount(object source,EventArgs e) //事件处理程序的签名必须与委托的签名匹配
        {
            DozensCount++;
        }
    }
    class chapt15_6UsageOfStandardEvents
    {
        static void _Main()
        {
            Incrementer incrementer = new Incrementer();
            Dozens dozensCounter = new Dozens(incrementer);

            incrementer.DoCount();
            Console.WriteLine("Number of dozens = {0}", dozensCounter.DozensCount);
        }
    }
}
